package uk.gov.swmprobation.cplite;

import android.annotation.TargetApi;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Display;
import android.view.MenuItem;


public class MyActivity extends ActionBarActivity implements ActionBar.TabListener {
	
	public static SharedPreferences sharedPref;
    public ActionBar actionBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
		sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		
		if (sharedPref.getInt("Max_Picture_Size", 0)==0){
			setPictureSizeMax();
		}
		
        actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);        
	}
	@Override
	protected void onResume(){
		super.onResume();
	}
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.menu_settings:
            	if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
            		startActivity(new Intent(this, SettingsActivity.class));
            	} else {
            		startActivity(new Intent(this, SettingsFragment.class));
            	}
                return true;
            case R.id.menu_about:
            	startActivity(new Intent(this,About.class));
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    
	@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
	private void setPictureSizeMax(){
		int width=0,height=0,max_size;
		Display display = getWindowManager().getDefaultDisplay();
		if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2){
			width = display.getWidth();
			height = display.getHeight();
		} else {
			Point size = new Point();
			display.getSize(size);
			width = size.x;
			height = size.y;
		}
		max_size = Math.round((Math.max(width, height)/4));
		
		sharedPref.edit().putInt("Max_Picture_Size", max_size).commit();
    }
    
	@Override
	public void onTabReselected(Tab tab,
			android.support.v4.app.FragmentTransaction arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTabSelected(Tab tab,
			android.support.v4.app.FragmentTransaction arg1) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTabUnselected(Tab tab,
			android.support.v4.app.FragmentTransaction arg1) {
		// TODO Auto-generated method stub
		
	}
}
